

package jetbrains.buildServer.server.rest.model.role;

import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import jetbrains.buildServer.server.rest.model.Fields;
import jetbrains.buildServer.server.rest.model.user.Permission;
import jetbrains.buildServer.server.rest.util.ValueWithDefault;
import jetbrains.buildServer.util.CollectionsUtil;
import org.jetbrains.annotations.NotNull;

public class Permissions {
  @XmlAttribute
  public Integer count;

  @XmlElement(name = "permission")
  public List<Permission> items;

  public Permissions() {
  }

  public Permissions(List<jetbrains.buildServer.serverSide.auth.Permission> permissions, @NotNull Fields fields) {
    items = ValueWithDefault.decideIncludeByDefault(
      fields.isIncluded("permission", true),
      CollectionsUtil.convertCollection(
        permissions,
        (source) -> new Permission(source, fields.getNestedField("permission"))
      )
    );
    count = ValueWithDefault.decideIncludeByDefault(
      fields.isIncluded("count", true),
      permissions.size()
    );
  }
}